package nc.bs.pub.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.bd.defdoc.IDefdocService;
import nc.itf.pu.m422x.IStoreReqAppMaintain;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.pu.m422x.context.StorereqContext;
import nc.vo.pu.m422x.entity.StoreReqAppHeaderVO;
import nc.vo.pu.m422x.entity.StoreReqAppItemVO;
import nc.vo.pu.m422x.entity.StoreReqAppVO;
import nc.vo.pu.pf.PUPFParameter;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.uap.pf.PFBusinessException;

public class N_422X_SAVEBASE extends AbstractCompiler2 {
	public String getCodeRemark() {
		return "  Object retValue = null;\nnc.vo.pu.m422x.entity.StoreReqAppVO[] inObject = (nc.vo.pu.m422x.entity.StoreReqAppVO[])getVos ();\nretValue = nc.bs.framework.common.NCLocator.getInstance().lookup(nc.itf.pu.m422x.IStoreReqAppMaintain.class).save(inObject);\nreturn retValue;\n";
	}

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		
		try {
			
			int flag=0;
			super.m_tmpVo = vo;
			Object retValue = null;
			StorereqContext context = StorereqContext.getInstance();
			PUPFParameter userObj = (PUPFParameter) this.getUserObj();
			if (null != userObj && userObj.isbBudgetCheckkFlag()) {
				context.setbBudgetControlCheckFlag(true);
			} else {
				context.setbBudgetControlCheckFlag(false);
			}
			
			List<DefdocVO> deList=new ArrayList<DefdocVO>();
			String sqlcheck="SELECT pk_defdoclist FROM  bd_defdoclist WHERE name ='申请单'";
			
			String doclist=(String) new BaseDAO().executeQuery(sqlcheck, new ColumnProcessor());
			
			
			StoreReqAppVO[] inObject = (StoreReqAppVO[]) ((StoreReqAppVO[]) this.getVos());
			for(StoreReqAppVO srvo :inObject) {
				StoreReqAppHeaderVO  srhdvo=(StoreReqAppHeaderVO) srvo.getParentVO();
				StoreReqAppItemVO[]	sraiVo=(StoreReqAppItemVO[]) srvo.getAllChildrenVO();
			for(StoreReqAppItemVO   srivo: sraiVo) {
			   //子集携带单号
				String material=srivo.getPk_material();				
				String sql="SELECT pob.norigtaxprice FROM po_order_b  pob LEFT JOIN po_order po" + 
						" ON pob.pk_order = po.pk_order WHERE  pob.pk_material= '"+material+"'    ORDER  BY  pob.dbilldate desc ";	
		        List<BigDecimal> messages=(List<BigDecimal>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());  		
				if(!messages.isEmpty()) {
					srivo.setVbdef2(messages.get(0).toString());
				}		
		
				//设置自定义档案
			   DefdocVO vos = new DefdocVO();
			   vos.setPk_group(srhdvo.getPk_group());
			   vos.setCreator(srhdvo.getCreator());
			   vos.setCreationtime(new UFDateTime());
			   vos.setTs(new UFDateTime());
			   vos.setPk_defdoclist(doclist);
			   vos.setDataoriginflag(0);
			   vos.setDatatype(1);
			   vos.setPk_org(srhdvo.getPk_group());
			   vos.setEnablestate(2);
			   deList.add(vos);
			  
				
			}
	     }		
			//humf,附件保存校验,有附件的单据不允许修改密级，采购合同
			String sql ="SELECT * FROM (select substr(filepath, INSTR(filepath, '/', 1, lengthb(regexp_replace(filepath, '[^/]', null))) - 20, 20) AS pk_bill from sm_pub_filesystem sm where ISFOLDER = 'n') pk WHERE  pk.pk_bill='"+inObject[0].getPrimaryKey()+"'  ";
            List<String> messages=(List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());				
			if(!messages.isEmpty()) {//有附件
				//查询是否有修改密级
				String mjsql = "SELECT vdef10 FROM po_storereq WHERE pk_storereq = '"+inObject[0].getPrimaryKey()+"'";
				List<String> dasmessages=(List<String>) new BaseDAO().executeQuery(mjsql, new ColumnListProcessor());//修改前密级
				String xghmj = inObject[0].getParentVO().getAttributeValue("vdef10")+"";//修改后密级
				if(!xghmj.equals(dasmessages.get(0))) {//密级有修改
					throw new BusinessException("单据存在附件时不可修改密级，请删除附件后重试");
				}
			}
			//end
			
			//lhw 231128 保存前校验项目 begin
			new projectFAUtils().projectDataApprove("po_storereq",inObject, "save");
			//end
			
 			StoreReqAppVO[] retValue1 = ((IStoreReqAppMaintain) NCLocator.getInstance().lookup(IStoreReqAppMaintain.class))
					.save(inObject);
			
			
	
		
		
	//wxp begin		
 			
	  StoreReqAppHeaderVO strvo=(StoreReqAppHeaderVO) retValue1[0].getParentVO();
//	  	  
//
	  StoreReqAppItemVO[]	sraiVo=(StoreReqAppItemVO[]) retValue1[0].getChildrenVO();
	  
	  
	  StoreReqAppHeaderVO  srhdvo=(StoreReqAppHeaderVO)  inObject[0].getParentVO();
      String codex=null;
			if(!deList.isEmpty()) {
         for(  DefdocVO doc:deList) {
        	 
        	  DefdocVO[] docv=new DefdocVO[1];
        	  if(srhdvo.getVbillcode()!=null) {
        	  doc.setCode(srhdvo.getVbillcode());
        	  doc.setName(srhdvo.getVbillcode());
        	  }else {
        		  doc.setCode(strvo.getVbillcode());
            	  doc.setName(strvo.getVbillcode());
        	  }
        	  doc.setPk_org(doc.getPk_group());
        	  
        	  
        	  docv[0]=doc;
        	
				try {
					
					String sqls="SELECT bd.code  FROM  bd_defdoc bd LEFT JOIN bd_defdoclist bdl ON bd.pk_defdoclist =bdl.pk_defdoclist  WHERE bdl.code='715SQD' and bd.dr=0 and bd.code='"+doc.getCode()+"'";
					
					String codes=(String) new BaseDAO().executeQuery(sqls,  new ColumnProcessor());  
					
					if(codes!=null&&flag==0) {
						flag=1;
					}
                   flag=flag+1;					
					if(flag==1) {
						((IDefdocService) NCLocator.getInstance().lookup(IDefdocService.class))
						.insertDefdocs(docv[0].getPk_org(), docv);
					}
				} catch (BusinessException e) {
				throw new BusinessException("自定义档案生成失败!!!!");
				}
				String sqlsx="SELECT bd.pk_defdoc  FROM  bd_defdoc bd LEFT JOIN bd_defdoclist bdl ON bd.pk_defdoclist =bdl.pk_defdoclist  WHERE bdl.code='715SQD' and bd.dr=0 and bd.code='"+doc.getCode()+"'";
			    codex=(String) new BaseDAO().executeQuery(sqlsx,  new ColumnProcessor());  
          }	
            
			
        
			}
			
			  
			  retValue1[0].setChildrenVO(sraiVo);
			 
			  StoreReqAppVO[] retValue2=((IStoreReqAppMaintain) NCLocator.getInstance().lookup(IStoreReqAppMaintain.class))
				.save(retValue1);
              
			  for(StoreReqAppItemVO sraVO:sraiVo) {
					StoreReqAppHeaderVO  srhdvox=(StoreReqAppHeaderVO)  inObject[0].getParentVO();
					String sqld="update po_storereq_b set vfree1='"+codex+"' where pk_storereq='"+strvo.getPk_storereq()+"'";
					new BaseDAO().executeUpdate(sqld);  
					sraVO.setVfree1(codex);				 
			  }
			  retValue2[0].setChildrenVO(sraiVo);
			  
			  //end
			return retValue2;
		} catch (Exception var6) {
			if (var6 instanceof BusinessException) {
				throw (BusinessException) var6;
			} else {
				throw new PFBusinessException(var6.getMessage(), var6);
			}
		}
	}
}